<?php {

    class Application_Model_ConvertKingTrans extends Application_Model_DomainObject {

        protected $_id;
        protected $_channel;
        protected $_weight;
        protected $_price;
        protected $_currency;
        protected $_expressTime;
        protected $_country;
        protected $_agentcompanyid;
        protected $_status;
        

        public function deleteAll($agentid, $country, $start, $end) {
            Application_Model_ConvertKingTrans::getDao()->delete("agentcompanyid=" . $agentid . " and country='" . $country . "'" . " and weight>=" . $start . " and weight<=" . $end);
        }

        public function ToExpressCost($kingtrans) {
            try {
                $this->getDbTable()->beginTran();

                $channelModel = Application_Model_ExpressChannel::getChannel($kingtrans->channel);

                if (is_null($channelModel)) {
                    $newChannel = new Application_Model_ExpressChannel();
                    $newChannel->name = $kingtrans->channel;
                    $newChannel->key = $kingtrans->channel;
                    $newChannel->save();
                    $channelid = $newChannel->id;
                }
                else
                    $channelid = $channelModel->id;


                $expresscost = new Application_Model_ExpressCost();
                $expresscost->agentCompanyId = $kingtrans->agentcompanyid;
                
                $city= Application_Model_city::getByWord($kingtrans->country);
     
                $expresscost->cityId=$city->id;
                $expresscost->weight = $kingtrans->weight;
                $expresscost->price = $kingtrans->price;
                $expresscost->channelId = $channelid;
                $expresscost->save();

                $kingtrans->status=1;
                $kingtrans->save();
                
                $this->getDbTable()->commit();
              
            } catch (Exception $ex) {
                $this->getDbTable()->rollBack();
                throw $ex;
            }
        }

        public function getSitePrice($agent, $country, $weight, $goodstype) {
            $objhly = new Uuch_KingTransAPI($agent->host, $agent->username, $agent->password);
            $filter = new searchFilter();
            $filter->country = $country;
            $filter->goodstype = $goodstype;
            $filter->rweight = $weight;
            $resulthly = $objhly->getPrice($filter);
            foreach ($resulthly as $item) {
                $model = new Application_Model_ConvertKingTrans();
                $model->_channel = $item->channel;
                $model->_weight = $item->weight;
                $model->_price = $item->price;
                $model->_currency = $item->currency;
                $model->_expressTime = $item->expressTime;
                $model->_country = $country;
                $model->_agentcompanyid = $agent->id;
                $model->save();
            }
        }

    }

}
?>
